package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.PhoneAccountLoginApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.model.LoginUser;
import com.kd.juhaowan.utils.LogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PhoneLoginAccountActivity extends BaseActivity {

    @BindView(R.id.forget_listview)
    ListView mListView;

    @BindView(R.id.login_account_title)
    TextView titleText;
    @BindView(R.id.login_account_tip)
    TextView tipText;
    private BaseAdapter mAdapter;

    private List<LoginUser> userList;
    private boolean isForget =false;
    private String phone;


    @OnClick(R.id.actvity_back)
    public void onBackclick(){
        finish();
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_account_login);
        if(getIntent().hasExtra("isForget")){
            isForget = getIntent().getBooleanExtra("isForget",false);
            phone = getIntent().getStringExtra("phone");
//            code  = getIntent().getStringExtra("code");
        }
        userList = (List<LoginUser>)getIntent().getSerializableExtra("user");
        if(isForget){
            titleText.setText("确认找回密码账号");
            tipText.setText("您的手机号"+phone+"绑定了多个账号，请确认需要找回密码的账号");
        }else{
            titleText.setText("选择登录账号");
            tipText.setText("您的手机号"+phone+"绑定了多个账号，请选择需要登录的账号");
        }
        setAdapter();
    }

    @Override
    protected void reInitDate() {

    }

    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_forget_account;
    }

//    @OnClick(R.id.login_account_forget)
//    public void onForgetClick(View view){
//
//    }
private int selectIndex =0;
    @OnClick(R.id.login_confirm)
    public void onLoginClick(View view){
//        startActivity(new Intent(ForgetActivity.this,ForgetFindActivity.class));
//        AlertDialog dialog = new AlertDialog.Builder(PhoneLoginAccountActivity.this).create();
//        dialog.show();
        LoginUser user = userList.get(selectIndex);
        if(!isForget){
            EasyHttp.post(this).api(new PhoneAccountLoginApi(user.getUsername(),user.getToken())).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
                @Override
                public void onHttpSuccess(HttpData<LoginUser> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
                    LogUtil.debugLog(result.getData().getAccess_token());
                    LoginUser user = result.getData();
                    user.setAccount(user.getUsername());
                    user.setPassword("xxxx");
                    user.setPhoneAccount(false);
                    manager.saveUserAccount(result.getData());
                    manager.writeAutoLogin(true);
                    manager.writeLastLoginType(1);
//                    manager.saveCurrentLoginUserTickit(user.getAccess_token());
                    manager.saveCurrentLoginUserTickit(result.getData().getAccess_token());
                    setResult(RESULT_OK);
                    finish();
                }
            });
        }else {
            Intent intent =new Intent(PhoneLoginAccountActivity.this,ForgetFindActivity.class);
            intent.putExtra("phone",phone);
            intent.putExtra("token",user.getToken());
            intent.putExtra("username",user.getUsername());
            startActivity(intent);
        }

    }

    private void setAdapter(){
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(i!=selectIndex){
                    selectIndex =i;
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
        mAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return userList.size();
            }

            @Override
            public Object getItem(int i) {
                return userList.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View convertView, ViewGroup parent) {
                ViewHolder viewHolder = null;
                if (convertView == null) {
                    viewHolder = new ViewHolder();

                    convertView = LayoutInflater.from(PhoneLoginAccountActivity.this).inflate(R.layout.item_chooseaccount, null, false);
                    viewHolder.account = convertView.findViewById(R.id.item_choosefriend_name);
                    viewHolder.time = convertView.findViewById(R.id.item_choosefriend_guanzhu);
                    viewHolder.checkView = convertView.findViewById(R.id.item_choosefriend_box);

                    convertView.setTag(viewHolder);
                }else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                LoginUser account = userList.get(i);
                viewHolder.account.setText(account.getUsername());
                LogUtil.debugLog("上次登录 "+account.getLogintime());
                viewHolder.time.setText("上次登录: "+account.getLogintime());

                viewHolder.checkView.setImageResource(i==selectIndex?R.mipmap.icon_account_checked:R.mipmap.icon_account_uncheck);
                return convertView;
            }};
        mListView.setAdapter(mAdapter);
    }

    class ViewHolder {

        TextView account;
        ImageView checkView;
        TextView time;


    }


}
